package com.mcptt.config.body.pidf;

import android.util.Xml;

import com.cloudptt.api.product.func.crsc.FuncNumOb;
import com.cloudptt.api.product.log.Log;

import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Vector;

public class PidfXmlBody {
	public static final int Type_Init_Logx = 0;
	public static final int Type_Query = 1;
	public static final int Type_Compulsory = 2;
	private static final String End_Mcptt_Tag = "</presence>";
	private static final String Name_Tuple = "tuple";
	private static final String Name_Id = "id";
	private static final String Name_Func = "functionalAlias";
	private static final String Name_FunctionalAliasID = "functionalAliasID";
	private static final String Name_TakeOver = "take-over";
	private static final String Name_User = "user";
	private static final String Name_Status = "status";
	private static final String Val_Status_Activated = "activated";
	private static final String Val_True = "true";
	private static final String Name_TriggerId = "TriggerId";

	private String uuid = null;
	private String mcpttId = "";
	private String funcNum =null;
	private String user =null;
	private boolean isForce = false;
	private String initiateId = null;
	private String pidfId = "pidfa01";
	private boolean isActive = true;
	private int type = Type_Query;

	private List<FuncNumOb> funcNameList = null;


	public PidfXmlBody(String uuid,String mcpttId,String funcNum,boolean isForce,String initiateId)
	{
		this.uuid = uuid;
		this.mcpttId = mcpttId;
		this.funcNum = funcNum;
		this.isForce = isForce;
		this.initiateId = initiateId;
	}
	private PidfXmlBody()
	{
	}

	public static PidfXmlBody parseXmlStr(String xmlStr) throws Exception
	{
		PidfXmlBody body = new PidfXmlBody();

		int index = xmlStr.indexOf(End_Mcptt_Tag);
		xmlStr= xmlStr.substring(0, index + End_Mcptt_Tag.length());
		xmlStr = xmlStr.trim();

		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据
		int event = pullParser.getEventType();
		body.funcNameList = new Vector();
		while (event != XmlPullParser.END_DOCUMENT){
			switch (event) {

				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					Log.debug(PidfXmlBody.class,"parseXmlStr = " + pullParser.getName());
					if (Name_Tuple.equals(pullParser.getName()))
					{
						body.uuid = pullParser.getAttributeValue("", Name_Id);
						Log.debug(PidfXmlBody.class,"parseXmlStr body.uuid = " + body.uuid);
					}
					else if (Name_Func.equals(pullParser.getName()))
					{
						String funcNum = pullParser.getAttributeValue("", Name_FunctionalAliasID);
						if(funcNum != null)
						{
							body.funcNum = funcNum;
						}
						String status = pullParser.getAttributeValue("", Name_Status);
						boolean isActive = false;
						if(status != null)
						{
							if (Val_Status_Activated.equals(status)) {
								body.isActive = isActive = true;

							} else {
								body.isActive = isActive = false;
							}
						}
						FuncNumOb ob = new FuncNumOb(funcNum,isActive);
						String user = pullParser.getAttributeValue("", Name_User);
						ob.setUser(user);
						Log.debug(PidfXmlBody.class,"parseXmlStr funcNum = " + funcNum + " status = " + status + " user = " + user);
						if(user == null
							|| user.equals(""))
						{
							body.type = Type_Init_Logx;
						}
						else
						{
							body.user = user;
							if(body.type != Type_Compulsory) {
								body.type = Type_Query;
							}
						}
						Log.debug(PidfXmlBody.class,"parseXmlStr body.type = " + body.type);
						Log.debug(PidfXmlBody.class,"parseXmlStr ob = " + ob);
						body.funcNameList.add(ob);
					}
					else if (Name_TakeOver.equals(pullParser.getName()))
					{
						String value = pullParser.nextText();
						Log.debug(PidfXmlBody.class,"parseXmlStr value = " + value);
						if(value != null)
						{
							value = value.trim();
						}
						if(Val_True.equals(value))
						{
							body.type = Type_Compulsory;
						}
						Log.debug(PidfXmlBody.class,"parseXmlStr body.type = " + body.type);
					}
//					else if (Name_TravelledDistance.equals(pullParser.getName()))
//					{
//						body.mcpttId = pullParser.getAttributeValue("", Name_TriggerId);
//						String value = pullParser.nextText();
//						body.distanceInterval = CheckData.parseInt(value);
//					}
					break;

				case XmlPullParser.END_TAG:
					break;

			}

			event = pullParser.next();
		}
//		body.funcNameList = funcNameList;
		Log.debug(PidfXmlBody.class,"parseXmlStr end body.type = " + body.type + " body.funcNameList = " + body.funcNameList);

		return body;
	}
	public String toValueString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("uuid = ").append(uuid).append(",");
		strBuf.append("mcpttId = ").append(mcpttId).append(",");
		strBuf.append("funcNum = ").append(funcNum).append(",");
		strBuf.append("isForce = ").append(isForce).append(",");
		strBuf.append("initiateId = ").append(initiateId).append(",");
		strBuf.append("pidfId = ").append(pidfId).append(",");
		strBuf.append("isActive = ").append(isActive).append(",");
		strBuf.append("type = ").append(type).append(",");
		strBuf.append("funcNameList = ").append(funcNameList).append(".");

		return strBuf.toString();
	}
	public String toString()
	{

		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r\n");
		strBuff.append("<presence entity=\"").append(mcpttId).append("\">").append("\r\n");
		strBuff.append("<tuple id=\"").append(uuid).append("\">").append("\r\n");
		strBuff.append("<status>").append("\r\n");
		Log.debug(this.getClass(),"toString isForce = " + isForce);
		if(isForce)
		{
			strBuff.append("<mcpttPIFA10:functionalAlias functionalAliasID=\"").append(funcNum).append("\"  user=\"").append(initiateId).append("\"/>").append("\r\n");
			strBuff.append("<take-over>true</take-over>").append("\r\n");
		}
		else
		{
			strBuff.append("<mcpttPIFA10:functionalAlias functionalAliasID=\"").append(funcNum).append("\"/>").append("\r\n");
		}
		strBuff.append("</status>").append("\r\n");
		strBuff.append("</tuple>").append("\r\n");
		strBuff.append("<mcpttPIFA10:p-id-fa>").append(pidfId).append("</mcpttPIFA10:p-id-fa>").append("\r\n");
		strBuff.append("</presence>").append("\r\n");
		
		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return strBuff.toString();
	}

	public String getUuid() {
		return uuid;
	}

	public String getFuncNum() {
		return funcNum;
	}

	public boolean isActive() {
		return isActive;
	}
	public String getUser() {
		return user;
	}
	public List<FuncNumOb> getFuncNameList() {
		return funcNameList;
	}
	public int getType() {
		return type;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
